﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;

namespace BLL
{
    public  class SetOpeningAmountBLL
    {
        /// <summary>
        /// 设置期初金额
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="Amount"></param>
        public static void SetCustomerOpeningAmount(string CustomerID,Decimal Amount,decimal invoice)
        {
            var Correspondencedetails=Connect.dbcontext.Queryable<LwDetail >().Where (x=>x.KHBH==CustomerID).OrderBy (x=>x.rq ).ToList ();
            decimal closingamount=Amount;
            foreach (var item in Correspondencedetails)
            {
                item.QichuJine = closingamount;
                //item.OpeningInvoices = invoice;             
                    item.QiMojine = item.QichuJine + item.AddYingshoukuan + item.AddYingFukuan - item.ReduceYingShouKuan - item.ReduceYingFuKuan;             
                //item.PeriodEndInvoices = item.OpeningInvoices + item.AddYingKaiFapiao - item.ReduceYingKaiFapiao;
                closingamount = item.QiMojine;
            }
            try
            {
                Connect.dbcontext.BeginTran();
                Connect.dbcontext.Updateable(Correspondencedetails).ExecuteCommand();
                var linkman = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == CustomerID); 
                if(linkman != null)
                {
                    linkman.JE = closingamount;
                    linkman.fp=Correspondencedetails.Sum(x=>x.AddYingKaiFapiao )-Correspondencedetails.Sum(x=>x.ReduceYingKaiFapiao )+invoice ;
                    Connect.dbcontext.Updateable(linkman).ExecuteCommand();
                }                
                Connect.dbcontext.CommitTran();
            }
            catch (Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                throw ex;
            }
        }
    }
}
